//
//  EMMTouristsViewController.m
//  EMMApp
//
//  Created by Mac on 16/4/29.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMTouristsViewController.h"
#import "DropDownListView.h"
#import "TouristsCell.h"
#import "AddCustomersViewController.h"

#import <MessageUI/MessageUI.h>
#import "EMMSearchViewController.h"
#import "ShowMenuListView.h"

#import "SWTableViewCell.h"
#import "ToutistsHomeViewCell.h"
#import "KnowViewController.h"
#import "SubscriptionViewController.h"
#import "TouristsModel.h"
#import "FollowUpViewController.h"
#import "FirstTouchController.h"
#import "GetCustomerListApi.h"
#import "GetConsultantListApi.h"
#import "DistributionConsultantApi.h"
#import "GetDictionaryListApi.h"
#import "EMMTouristsCell.h"
#import "EMMTouristsNewCell.h"
#import "EMMTouristsEditCell.h"
#import "YCXMenu.h"
#import "ProjectModel.h"
#import "FuzzySearchView.h"
#import "TouristsDetailWebViewController.h"
#import "NewCustomerWebViewController.h"


@interface EMMTouristsViewController ()<UITableViewDelegate,
                                        UITableViewDataSource,
                                        MFMessageComposeViewControllerDelegate,
                                        UIScrollViewDelegate,SWTableViewCellDelegate>
{
    NSMutableArray* tradeList;
    NSArray* icons;
    NSString *pageSize;
    NSInteger currentPage;
    NSMutableArray *chooseArray ;
  
    NSInteger current;
    NSInteger currentSub;
    NSInteger currentSubOne;
    NSString *saiXString;
    BOOL _isc1;
    BOOL _isc2;
    
    
    NSInteger touchIndex;
    UIBarButtonItem *leftItemButton;
    NSInteger integer;
    NSString *suanjiaString;
    BOOL _isTour;
    
    NSString *type;
    NSString *sort;
    NSString *screenType;
    NSString *screenValue;
    NSString *json;
    //判断导航
    BOOL _isShowUI;
    NSString *xmQXString;
    UITableViewRowAction *deleteRowAction;
    UITableViewRowAction *moreRowAction;
    UITableViewRowAction *sanRowAction;
    UITableViewRowAction *OK;
    BOOL isMenuPopoverShow;
    NSInteger jjrindex;
}

@property (nonatomic, strong) MenuLitsView *menuT;//导航
@property (nonatomic, strong) NSMutableArray *menuListT;//导航数组
@property (nonatomic, strong) NSMutableArray *menuArrT;//导航数组
@property (nonatomic, strong) NavigationView *navView;

@property (nonatomic, strong) NSArray *itmes;
@property (nonatomic, strong) NSArray *itmesDJ;
@property (nonatomic, strong) NSArray *itmeslist;
@property (nonatomic, strong) NSMutableArray *itmesNewArr;

@property (strong, nonatomic) UIView *headBackView;
@property (strong, nonatomic) UIImageView *headImageview;

@property (nonatomic, strong) UILabel *jlLabel; //当前没有记录

@property (nonatomic, strong) UITableView *touristsTableView;
@property (nonatomic, strong) GetConsultantList *getConsultantList;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *zyData;
@property (nonatomic, strong) NSMutableArray *zyDataArr;
@property (nonatomic, strong) NSMutableArray *zyDataArrId;

/**
 *  新增归属人
 *  归属数组
 *
 */
@property (nonatomic, strong) NSMutableArray *gsDataArr;
@property (nonatomic, strong) NSMutableArray *gsDataNameArr;


@property (nonatomic, strong) NSArray *menuItems;
@property (nonatomic, strong) NSArray *memImageViewArr;

@property (nonatomic, strong) ShowMenuListView *showView1;
@property (nonatomic, strong) ShowMenuListView *showView2;


@property (nonatomic, strong) UIButton *tourbtn;

@property (nonatomic, strong) NSMutableArray *seleArr;

@property (nonatomic, assign) int currentPageSize;// 当前页码


@property (nonatomic,strong) NSArray *yhArr;

@property (nonatomic,strong)UIButton *djBtn;//排序button

@property (nonatomic, strong)UIButton *btn;//筛选button

/**
 11、首次接触方式
 */
@property (nonatomic, strong) NSMutableArray *tcfsList;

//全选功能UIView
@property (nonatomic, strong) UIView *massSelectView;
//全选名称
@property (nonatomic, strong) UILabel *massSelectLabel;
//全选图片
@property (nonatomic, strong) UIImageView *massSelectImageV;
//判断是否全选
@property (nonatomic, assign) BOOL isMassSlect;
//选择是的数量
@property (nonatomic, strong) UILabel *massSelectNumLabel;

@property (nonatomic, assign) CGFloat topSet;
@end

@implementation EMMTouristsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArr = [[NSMutableArray alloc] init];
    _zyData = [[NSMutableArray alloc] init];
    _zyDataArr = [[NSMutableArray alloc] init];
    _gsDataArr = [[NSMutableArray alloc] init];
    _gsDataNameArr = [[NSMutableArray alloc] init];
    _tcfsList = [[NSMutableArray alloc] init];
    currentSub = 0;
    currentSubOne = 0;

    
    self.view.backgroundColor = [UIColor whiteColor];
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    _itmesNewArr = [[NSMutableArray alloc] init];
    NSString *isAddCustomer = [userdf objectForKey:@"isAddCustomer"];//客户菜单权限下按钮权限：客户新增
    NSString *isDistributionCustomer = [userdf objectForKey:@"isDistributionCustomer"];//客户菜单权限下按钮权限：客户分配
    [self creatDropDownView];
    if ([_suanjiaTpye isEqualToString:@"1"]) {
        integer = 2;
        suanjiaString = nil;
        [self suanjiaShowUI];
    }else {
        integer = 0;
        _seleArr = [[NSMutableArray alloc] init];
    }
    self.touristsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-SafeAreaBottomHeight-SafeAreaTopHeight) style:UITableViewStylePlain];
    self.touristsTableView.delegate = self;
    self.touristsTableView.dataSource = self;
    self.touristsTableView.rowHeight = 80;
    self.touristsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.touristsTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.touristsTableView];
    
    _jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, EMM_WIDTH, 20)];
    _jlLabel.textAlignment = NSTextAlignmentCenter;
    _jlLabel.text = @"当前没有记录";
    _jlLabel.font = [UIFont systemFontOfSize:16];
    _jlLabel.hidden = YES;
    [self.view addSubview:_jlLabel];
    if ([_tourSring isEqualToString:@"0"]|| [_tourSring isEqualToString:@"1"] || [_tourSring isEqualToString:@"3"] || [_tourSring isEqualToString:@"4"]) {
        if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"1"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"首触",@"写跟进", nil];
        }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"1"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"首触",@"写跟进", nil];
        }else if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"0"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"首触",@"写跟进", nil];
        }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"0"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"首触",@"写跟进", nil];
        }
//        _menuItems = [NSArray arrayWithObjects:@"搜索", @"群发短信",nil];
//        _memImageViewArr = [NSArray arrayWithObjects:@"more_search",@"more_message", nil];
        _itmesNewArr = [@[
                          [YCXMenuItem menuItem:@"群发短信   "
                                          image:[UIImage imageNamed:@"more_message"]
                                            tag:100
                                       userInfo:nil],
                          ] mutableCopy];
    }else {
        if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"1"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"首触",@"写跟进",@"分配", nil];
//            _menuItems = [NSArray arrayWithObjects:@"搜索", @"群发短信", @"新增客户", @"批量分配",nil];
//            _memImageViewArr = [NSArray arrayWithObjects:@"more_search",@"more_message",@"more_add",@"more_allot", nil];
            _itmesNewArr = [[NSMutableArray alloc]initWithArray:@[[YCXMenuItem menuItem:@"群发短信   "
                                                                                  image:[UIImage imageNamed:@"more_message"]
                                                                                    tag:100
                                                                               userInfo:nil],[YCXMenuItem menuItem:@"批量分配"
                                                                                                             image:[UIImage imageNamed:@"more_allot"]
                                                                                                               tag:100
                                                                                                          userInfo:nil]]];

        }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"1"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"发短信",@"写跟进",@"分配", nil];
//            _menuItems = [NSArray arrayWithObjects:@"搜索", @"群发短信", @"批量分配",nil];
//            _memImageViewArr = [NSArray arrayWithObjects:@"more_search",@"more_message",@"more_allot", nil];
            _itmesNewArr = [@[
                              [YCXMenuItem menuItem:@"群发短信   "
                                              image:[UIImage imageNamed:@"more_message"]
                                                tag:100
                                           userInfo:nil],
                              [YCXMenuItem menuItem:@"批量分配"
                                              image:[UIImage imageNamed:@"more_allot"]
                                                tag:100
                                           userInfo:nil],
                              ] mutableCopy];
        }else if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"0"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"首触",@"写跟进", nil];
//            _menuItems = [NSArray arrayWithObjects:@"搜索", @"群发短信", @"新增客户",nil];
//            _memImageViewArr = [NSArray arrayWithObjects:@"more_search",@"more_message",@"more_add",nil];
            _itmesNewArr = [@[
                              [YCXMenuItem menuItem:@"群发短信   "
                                              image:[UIImage imageNamed:@"more_message"]
                                                tag:100
                                           userInfo:nil],
                              ] mutableCopy];
        }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"0"]) {
            _yhArr = [NSArray arrayWithObjects:@"打电话",@"首触",@"写跟进", nil];
//            _menuItems = [NSArray arrayWithObjects:@"搜索", @"群发短信",nil];
//            _memImageViewArr = [NSArray arrayWithObjects:@"more_search",@"more_message",nil];
            _itmesNewArr = [@[
                              [YCXMenuItem menuItem:@"群发短信   "
                                              image:[UIImage imageNamed:@"more_message"]
                                                tag:100
                                           userInfo:nil],
                              ] mutableCopy];
        }

    }
    self.touristsTableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadNewData];
    }];
    self.touristsTableView.mj_header.automaticallyChangeAlpha = YES;
    self.touristsTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         [self loadMoreData];
    }];
    
     [self.touristsTableView.mj_header beginRefreshing];
    
    // 设置筛选项 默认索引
    NSString *necessary = [userdf objectForKey:@"necessary"];
    if ([necessary isEqualToString:@"1"]) {
        current = 2;
        sort = @"3";
    }else {
        current = 1;
        sort = @"2";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 算价界面跳转 
- (void)suanjiaShowUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT-60-SafeAreaTopHeight, EMM_WIDTH, 60)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIButton *doneQDBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    doneQDBtn.frame = CGRectMake(20, 5, EMM_WIDTH-40, 50);
    doneQDBtn.backgroundColor = [UIColor orangeColor];
    [doneQDBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneQDBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneQDBtn addTarget:self action:@selector(doneQDBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    doneQDBtn.layer.masksToBounds = YES;
    doneQDBtn.layer.cornerRadius = 5;
    [view addSubview:doneQDBtn];
}

- (void)doneQDBtnClick:(UIButton *)btn {
    if ([NSString isStringEmpty2:suanjiaString] == YES) {
        [self showToast:@"请先选择客户"];
        return;
    }
    CustomerList *list = [_dataArr objectAtIndex:[suanjiaString integerValue]];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [arr addObject:list];
    if (_touristsBlock) {
        _touristsBlock(arr);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - View
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[EMMAppChecker sharedInstance] startCheckVgoUpdate];
    

    if ([_tourSring isEqualToString:@"0"] || [_tourSring isEqualToString:@"1"] || [_tourSring isEqualToString:@"3"] || [_tourSring isEqualToString:@"4"]) {
        
    }else {
        _menuListT = [[NSMutableArray alloc] init];
        _menuArrT = [[NSMutableArray alloc] init];
        [self postGetProjectListApi];
    }
    
    [self postGetConsultantListApi];
    [self creatNavigationController];
//    _touristsTableView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self creatNavCVRemoveFromSuperview];
    [self btnLeftItemClick:nil];
}
//- (void)viewDidDisappear:(BOOL)animated {
//    [super viewDidDisappear:animated];
//}

- (void)creatNavCVRemoveFromSuperview {
    [_navView removeFromSuperview];
    _navView = nil;
}

- (void)creaShowUI:(NSInteger)num tcfsList:(NSMutableArray *)tcfsArr {
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    NSString *necessary = [userdf objectForKey:@"necessary"];
    if (num == 1) {
        if ([_tourSring isEqualToString:@"0"]|| [_tourSring isEqualToString:@"1"] || [_tourSring isEqualToString:@"3"] || [_tourSring isEqualToString:@"4"]) {
            if ([necessary isEqualToString:@"1"]) {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新", @"暂未跟进(默认)",@"按客户等级", nil];
            }else {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新(默认)", @"最新跟进",@"按客户等级", nil];
            }
            _itmesDJ = [NSArray arrayWithObjects:@"客户状态", @"意向等级", @"首次接触方式", nil];
            //是否已开通认筹功能：0、未开通；1、已开通
            NSArray *arr1 = [[NSArray alloc] init];
            NSString *isPaydeposit = [userdf objectForKey:@"isPaydeposit"];
            if ([isPaydeposit isEqualToString:@"0"]) {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认购",@"签约",@"无意向",nil];
            }else {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
            }
            NSArray *arr2 = [NSArray arrayWithObjects:@"全部",@"A", @"B", @"C", @"D",nil];
//            NSArray *arr4 = [NSArray arrayWithObjects:@"来电", @"来访", @"其他",nil];
            _itmeslist = [NSArray arrayWithObjects:arr1, arr2, tcfsArr,nil];
        }else {
            if ([necessary isEqualToString:@"1"]) {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新", @"暂未跟进(默认)", @"按标星",@"按客户等级", nil];
            }else {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新(默认)", @"最新跟进", @"按标星",@"按客户等级", nil];
            }
            _itmesDJ = [NSArray arrayWithObjects:@"客户状态", @"意向等级", @"跟进时间", @"首次接触方式",@"归属人", nil];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            //是否已开通认筹功能：0、未开通；1、已开通
            NSArray *arr1 = [[NSArray alloc] init];
            NSString *isPaydeposit = [userdf objectForKey:@"isPaydeposit"];
            if ([isPaydeposit isEqualToString:@"0"]) {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认购",@"签约",@"无意向",nil];
            }else {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
            }
//            NSArray *arr1 = [NSArray arrayWithObjects:@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
            NSArray *arr2 = [NSArray arrayWithObjects:@"全部",@"A", @"B", @"C", @"D",nil];
            NSArray *arr3 = [NSArray arrayWithObjects:@"全部",@"3天未跟进", @"一周未跟进", @"两周未跟进", @"一月未跟进", @"两月未跟进",nil];
//            NSArray *arr4 = [NSArray arrayWithObjects:@"来电", @"来访", @"其他",nil];
            if (_gsDataNameArr.count > 0) {
                _itmeslist = [NSArray arrayWithObjects:arr1, arr2, arr3, tcfsArr,_gsDataNameArr,nil];
            }else {
                NSMutableArray *arr5 = [[NSMutableArray alloc] init];
                NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
                NSString *userName = [userdf objectForKey:@"userName"];
                [arr5 addObject:userName];
                _itmeslist = [NSArray arrayWithObjects:arr1, arr2, arr3, tcfsArr,arr5,nil];
                
            }
        }
        
    }else if (num == 2) {
        if ([_tourSring isEqualToString:@"0"]|| [_tourSring isEqualToString:@"1"] || [_tourSring isEqualToString:@"3"] || [_tourSring isEqualToString:@"4"]) {
            if ([necessary isEqualToString:@"1"]) {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新", @"暂未跟进(默认)", nil];
            }else {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新(默认)", @"最新跟进", nil];
            }
            _itmesDJ = [NSArray arrayWithObjects:@"客户状态", @"意向等级", @"首次接触方式", nil];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            //是否已开通认筹功能：0、未开通；1、已开通
            NSArray *arr1 = [[NSArray alloc] init];
            NSString *isPaydeposit = [userdf objectForKey:@"isPaydeposit"];
            if ([isPaydeposit isEqualToString:@"0"]) {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认购",@"签约",@"无意向",nil];
            }else {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
            }
//            NSArray *arr1 = [NSArray arrayWithObjects:@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
            NSArray *arr2 = [NSArray arrayWithObjects:@"全部", @"A", @"B", @"C", @"D",nil];
//            NSArray *arr4 = [NSArray arrayWithObjects:@"来电", @"来访", @"其他",nil];
            _itmeslist = [NSArray arrayWithObjects:arr1, arr2,tcfsArr,nil];
        }else {
            if ([necessary isEqualToString:@"1"]) {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新", @"暂未跟进(默认)", @"按标星",@"按客户等级", nil];
            }else {
                _itmes = [NSArray arrayWithObjects:@"最新创建", @"最新更新(默认)", @"最新跟进", @"按标星",@"按客户等级", nil];
            }
            _itmesDJ = [NSArray arrayWithObjects:@"客户状态", @"意向等级", @"跟进时间", @"首次接触方式",@"归属人", nil];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
//            //是否已开通认筹功能：0、未开通；1、已开通
//            NSString *isPaydeposit = [userdf objectForKey:@"isPaydeposit"];
//            if ([isPaydeposit isEqualToString:@"0"]) {}else {
//                
//            }
            //是否已开通认筹功能：0、未开通；1、已开通
            NSArray *arr1 = [[NSArray alloc] init];
            NSString *isPaydeposit = [userdf objectForKey:@"isPaydeposit"];
            if ([isPaydeposit isEqualToString:@"0"]) {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认购",@"签约",@"无意向",nil];
            }else {
                arr1 = [NSArray arrayWithObjects:@"全部",@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
            }
//            NSArray *arr1 = [NSArray arrayWithObjects:@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
            NSArray *arr2 = [NSArray arrayWithObjects:@"全部", @"A", @"B", @"C", @"D",nil];
            NSArray *arr3 = [NSArray arrayWithObjects:@"全部",@"3天未跟进", @"一周未跟进", @"两周未跟进", @"一月未跟进", @"两月未跟进",nil];
//            NSArray *arr4 = [NSArray arrayWithObjects:@"来电", @"来访", @"其他",nil];
            NSMutableArray *arr5 = [[NSMutableArray alloc] init];
            _itmeslist = [NSArray arrayWithObjects:arr1, arr2, arr3, tcfsArr,arr5,nil];
        }
        
    }
}

- (void)postGetProjectListApi {
    [_menuArrT removeAllObjects];
    [_menuListT removeAllObjects];
    NSUserDefaults *us = [NSUserDefaults standardUserDefaults];
    NSMutableArray *projectDataArray = [us objectForKey:@"projectArray"];
    if (projectDataArray.count > 0 ) {
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (int i = 0; i < projectDataArray.count; i++) {
            NSData *data = projectDataArray[i];
            ProjectModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [_menuListT addObject:model.projectName];
            ProjectList *lsit = [[ProjectList alloc] init];
            lsit.projectName = model.projectName;
            lsit.projectId = model.projectId;
            lsit.projectCode = model.projectCode;
            [_menuArrT addObject:lsit];
            [arr addObject:lsit.projectCode];
        }
        NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
        NSString *projectCode = [userdf objectForKey:@"projectCode"];
        for (int i = 0; i < arr.count; i++) {
            NSString *pro = [arr objectAtIndex:i];
            if ([pro isEqualToString:projectCode]) {
            [userdf setObject:[NSString stringWithFormat:@"%d",i] forKey:@"indexString"];
            [userdf synchronize];
            }else {
            }
        }
    }
}
#pragma mark - 导航

- (void)creatNavigationController {
    [self updateNavBar];
    if ([_tourSring isEqualToString:@"0"]) {
        self.navigationItem.title = _titleString;
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
        [backBtn addTarget:self
                    action:@selector(backToPreviousPage:)
          forControlEvents:UIControlEventTouchUpInside];
        [backBtn setBackgroundColor:[UIColor clearColor]];
        [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        if (EMM_WIDTH <= 320) {
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }else if (EMM_WIDTH > 375){
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        }else{
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
        self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-SafeAreaTopHeight);
    }else if ([_tourSring isEqualToString:@"1"]) {
        self.navigationItem.title = _titleString;
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
        [backBtn addTarget:self
                    action:@selector(backToPreviousPage:)
          forControlEvents:UIControlEventTouchUpInside];
        [backBtn setBackgroundColor:[UIColor clearColor]];
        [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        if (EMM_WIDTH <= 320) {
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }else if (EMM_WIDTH > 375){
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        }else{
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-SafeAreaTopHeight);
    }else if ([_tourSring isEqualToString:@"3"]) {
        self.navigationItem.title = _titleString;
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
        [backBtn addTarget:self
                    action:@selector(backToPreviousPage:)
          forControlEvents:UIControlEventTouchUpInside];
        [backBtn setBackgroundColor:[UIColor clearColor]];
        [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        if (EMM_WIDTH <= 320) {
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }else if (EMM_WIDTH > 375){
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        }else{
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-SafeAreaTopHeight);
    }else if ([_tourSring isEqualToString:@"4"]) {
        self.navigationItem.title = _titleString;
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
        [backBtn addTarget:self
                    action:@selector(backToPreviousPage:)
          forControlEvents:UIControlEventTouchUpInside];
        [backBtn setBackgroundColor:[UIColor clearColor]];
        [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        if (EMM_WIDTH <= 320) {
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }else if (EMM_WIDTH > 375){
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        }else{
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        }
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-SafeAreaTopHeight-60);
    } else  {
        NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
        NSString *token = [userdf objectForKey:@"is_login"];
        NSString *projectName;
        if ([NSString isStringEmpty2:token] == NO) {
            projectName  = [userdf objectForKey:@"projectName"];
        }
        _navView = [[NavigationView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH/2, 44) title:projectName colo:@"2"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button addTarget:self action:@selector(showPopover:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        button.frame = CGRectMake(_navView.frame.size.width/2-50, 10, _navView.frame.size.width/2, 30.0);
        button.frame = CGRectMake(0, 0, _navView.frame.size.width, 44.0);
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        button.tag = 101;
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_navView addSubview:button];
        [self.navigationItem setTitleView:_navView];
        self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight);
       
        NSString *isAddCustomer = [userdf objectForKey:@"isAddCustomer"];//客户菜单权限下按钮权限：客户新增
        if ([isAddCustomer isEqualToString:@"1"]) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"addClient"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(addNewUserClick)];
        }
        
    }
    UIBarButtonItem *leftItme = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ioc_3"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightClick:)];
  
//    UIButton *rightItme = [UIButton buttonWithType:UIButtonTypeCustom];
//    [rightItme setFrame:CGRectMake(0, 0, 54 ,44)];
//    [rightItme addTarget:self
//                action:@selector(rightClick:)
//      forControlEvents:UIControlEventTouchUpInside];
//    [rightItme setBackgroundColor:[UIColor clearColor]];
//    [rightItme setImage:[UIImage imageNamed:@"ioc_3"] forState:UIControlStateNormal];
//    rightItme.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    [rightItme layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:0];
//    if (EMM_WIDTH <= 320) {
//        rightItme.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -5);
//    }else if (EMM_WIDTH > 375){
//        rightItme.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
//    }else{
//        rightItme.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -5);
//    }
    
    if ([self.suanjiaTpye isEqualToString:@"1"] || self.tourSring || self.tpye) {
        
    }else {
        self.navigationItem.leftBarButtonItem = leftItme;
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItme];
    }
}


#pragma mark - 显示app商家列表

- (void)showPopover:(UIButton *)button {
    [YCXMenu dismissMenu];
    [self btnLeftItemClick:nil];
    [self removeSubView];
    if (_isShowUI == YES) {
        [self removeMenuLitsViewSubView];
        [self creatView];
        _isShowUI = NO;
    }else {
        [self removeMenuLitsViewSubView];
        _isShowUI = YES;
    }
}

- (void)creatView {
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    NSString *num = [userdf objectForKey:@"indexString"];
    NSInteger h = 0;
    if (_menuListT.count > 6) {
        h = 6*50;
    }else {
        h = _menuListT.count * 50;
    }
    _menuT = [[MenuLitsView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) items:_menuListT indet:num isShowUI:YES guodu:h];
    _menuT.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:_menuT];
    __weak MenuLitsView *weakSelf = _menuT;
    [weakSelf setMenuLitsBlock:^(NSString *title, NSString *indet) {
        if ([NSString isStringEmpty2:title] == NO) {
            _isShowUI = YES;
        }else {
            _isShowUI = YES;
        }
        if ([NSString isStringEmpty2:indet] == NO) {
            ProjectList *lsit1 = _menuArrT[[indet intValue]];
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            [userdf setObject:lsit1.projectCode forKey:@"projectCode"];
            [userdf setObject:lsit1.projectId forKey:@"projectId"];
            [userdf setObject:lsit1.projectName forKey:@"projectName"];
            [userdf setObject:indet forKey:@"indexString"];
            [userdf synchronize];
            [self creatNavigationController];
            NSString *isDistributionCustomer = [userdf objectForKey:@"isDistributionCustomer"];//客户菜单权限下按钮权限：客户分配
            if ([isDistributionCustomer isEqualToString:@"1"]) {
            }
            [self postGetConsultantListApi];
            screenType = nil;
            screenValue = nil;
            currentSub = 0;
            currentSubOne = 0;
            NSString *necessary = [userdf objectForKey:@"necessary"];
            if ([necessary isEqualToString:@"1"]) {
                current = 2;
                sort = @"3";
            }else {
                current = 1;
                sort = @"2";
            }
            [self.touristsTableView.mj_header beginRefreshing];
            [[MerchantResourceVModel merchantResourceVModel] startMerchantResourceVModel];

        }
        [weakSelf removeFromSuperview];
    }];
}

- (void)removeMenuLitsViewSubView {
    [_menuT removeFromSuperview];
    _menuT = nil;
   
}

#pragma mark - 导航左按钮响应事件 -----

- (void)backToPreviousPage:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
    [self removeSubView];
}

#pragma mark - 导航右按钮响应事件 -----
- (void)addNewUserClick {
    NewCustomerWebViewController *nvc = [[NewCustomerWebViewController alloc] init];
    nvc.hidesBottomBarWhenPushed = YES;
    nvc.isflg = NO;
    [self.navigationController pushViewController:nvc animated:YES];
}

- (void)rightClick:(UIButton *)btn {
    [self removeMenuLitsViewSubView];
    [self removeSubView];
    [_btn setImage:[UIImage imageNamed:@"shaixuan_up"] forState:UIControlStateNormal];
    [_djBtn setImage:[UIImage imageNamed:@"ico_jt_up"] forState:UIControlStateNormal];
    [YCXMenu setTintColor:[UIColor colorFromHexCode:@"#FB5B3B"]];
    [YCXMenu setSelectedColor:[UIColor colorFromHexCode:@"#FB5B3B"]];
    [YCXMenu setSeparatorColor:[UIColor colorFromHexCode:@"#CCCCCC"]];
    [YCXMenu setTitleFont:[UIFont systemFontOfSize:15]];
    if ([YCXMenu isShow]){
        [YCXMenu dismissMenu];
    } else {
        [YCXMenu showMenuInView:self.view fromRect:CGRectMake(15, 0, 50, 0) menuItems:_itmesNewArr selected:^(NSInteger index, YCXMenuItem *item) {
            [self creatYCMenSeleIndex:index];
        }];
    }
}

#pragma mark - 导航右按钮弹出选择框选中回调
/**
 *  功能描述：
 *  群发短信全选功能显示全方法调用
 *  创建时间：16/4/20
 *
 *  更新时间：2017/02/14 下午
 *  更新人： wangguiping
 */
- (void)creatYCMenSeleIndex:(NSInteger)selectedIndex {
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    NSString *isAddCustomer = [userdf objectForKey:@"isAddCustomer"];//客户菜单权限下按钮权限：客户新增
    NSString *isDistributionCustomer = [userdf objectForKey:@"isDistributionCustomer"];//客户菜单权限下按钮权
    if ([_tourSring isEqualToString:@"0"]|| [_tourSring isEqualToString:@"1"] || [_tourSring isEqualToString:@"3"] || [_tourSring isEqualToString:@"4"]) {
        //群发短信
        [self creatChangeinitWithShowUI];
        _isTour = YES;
        self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight-50);
        [self massSelectViewInitWithShowUI];
//        if (selectedIndex == 0) {
            NSLog(@"0");
            // 搜索页面跳转
//            EMMSearchViewController *nvc = [[EMMSearchViewController alloc] init];
//            nvc.hidesBottomBarWhenPushed = YES;
//            nvc.tpye = self.tpye;
//            [self.navigationController pushViewController:nvc animated:YES];
//        }else if(selectedIndex == 1){
//            //群发短信
//            [self creatChangeinitWithShowUI];
//            _isTour = YES;
//            self.touristsTableView.frame = CGRectMake(0, TopSet, EMM_WIDTH, EMM_HEIGHT-TopSet-49-SafeAreaTopHeight-50);
//            [self massSelectViewInitWithShowUI];
//        }
    }else {
        if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"1"]) {
            if (selectedIndex == 0) {
                //群发短信
                [self creatChangeinitWithShowUI];
                _isTour = YES;
                self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight-50);
                [self massSelectViewInitWithShowUI];
//                NSLog(@"0");
//                EMMSearchViewController *nvc = [[EMMSearchViewController alloc] init];
//                nvc.hidesBottomBarWhenPushed = YES;
//                nvc.tpye = self.tpye;
//                [self.navigationController pushViewController:nvc animated:YES];
            }else if(selectedIndex == 1){
                [self creatChangeinitWithShowUI];
                _isTour = NO;
                self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight-50);
                [self massSelectViewInitWithShowUI];
//                //群发短信
//                [self creatChangeinitWithShowUI];
//                _isTour = YES;
//                self.touristsTableView.frame = CGRectMake(0, TopSet, EMM_WIDTH, EMM_HEIGHT-TopSet-49-SafeAreaTopHeight-50);
//                [self massSelectViewInitWithShowUI];
            }else if(selectedIndex == 2){
//                AddCustomersViewController *nvc = [[AddCustomersViewController alloc] init];
//                nvc.hidesBottomBarWhenPushed = YES;
//                nvc.isflg = NO;
//                [self.navigationController pushViewController:nvc animated:YES];
            }else if(selectedIndex == 3){
//                [self creatChangeinitWithShowUI];
//                _isTour = NO;
//                self.touristsTableView.frame = CGRectMake(0, TopSet, EMM_WIDTH, EMM_HEIGHT-TopSet-49-SafeAreaTopHeight-50);
//                [self massSelectViewInitWithShowUI];
            }
         }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"1"]) {
             if (selectedIndex == 0) {
                 //群发短信
                 [self creatChangeinitWithShowUI];
                 _isTour = YES;
                 self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight-50);
                 [self massSelectViewInitWithShowUI];
//                 NSLog(@"0");
//                 EMMSearchViewController *nvc = [[EMMSearchViewController alloc] init];
//                 nvc.hidesBottomBarWhenPushed = YES;
//                 nvc.tpye = self.tpye;
//                 [self.navigationController pushViewController:nvc animated:YES];
             }else if(selectedIndex == 1){
                 
                 [self creatChangeinitWithShowUI];
                 _isTour = NO;
                 self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight-50);
                 [self massSelectViewInitWithShowUI];
//                 //群发短信
//                 [self creatChangeinitWithShowUI];
//                 _isTour = YES;
//                 self.touristsTableView.frame = CGRectMake(0, TopSet, EMM_WIDTH, EMM_HEIGHT-TopSet-49-SafeAreaTopHeight-50);
//                 [self massSelectViewInitWithShowUI];
             }else if(selectedIndex == 2){
//                 [self creatChangeinitWithShowUI];
//                 _isTour = NO;
//                 self.touristsTableView.frame = CGRectMake(0, TopSet, EMM_WIDTH, EMM_HEIGHT-TopSet-49-SafeAreaTopHeight-50);
//                 [self massSelectViewInitWithShowUI];
             }
         }else if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"0"]) {
             if (selectedIndex == 0) {
                 //群发短信
                 [self creatChangeinitWithShowUI];
                 _isTour = YES;
                 self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight-50);
                 [self massSelectViewInitWithShowUI];
//                 NSLog(@"0");
//                 EMMSearchViewController *nvc = [[EMMSearchViewController alloc] init];
//                 nvc.hidesBottomBarWhenPushed = YES;
//                 nvc.tpye = self.tpye;
//                 [self.navigationController pushViewController:nvc animated:YES];
             }else if(selectedIndex == 1){
//                 //群发短信
//                 [self creatChangeinitWithShowUI];
//                 _isTour = YES;
//                 self.touristsTableView.frame = CGRectMake(0, TopSet, EMM_WIDTH, EMM_HEIGHT-TopSet-49-SafeAreaTopHeight-50);
//                 [self massSelectViewInitWithShowUI];
             }else if(selectedIndex == 2){
//                 AddCustomersViewController *nvc = [[AddCustomersViewController alloc] init];
//                 nvc.hidesBottomBarWhenPushed = YES;
//                 nvc.isflg = NO;
//                 [self.navigationController pushViewController:nvc animated:YES];
             }
         }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"0"]) {
             if (selectedIndex == 0) {
                 //群发短信
                 [self creatChangeinitWithShowUI];
                 _isTour = YES;
                 self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight-50);
                 [self massSelectViewInitWithShowUI];
                 
//                 NSLog(@"0");
//                 EMMSearchViewController *nvc = [[EMMSearchViewController alloc] init];
//                 nvc.hidesBottomBarWhenPushed = YES;
//                 nvc.tpye = self.tpye;
//                 [self.navigationController pushViewController:nvc animated:YES];
             }else if(selectedIndex == 1){
//                 //群发短信
//                 [self creatChangeinitWithShowUI];
//                 _isTour = YES;
//                 self.touristsTableView.frame = CGRectMake(0, TopSet, EMM_WIDTH, EMM_HEIGHT-TopSet-49-SafeAreaTopHeight-50);
//                 [self massSelectViewInitWithShowUI];
             }
         }
    }
}

#pragma mark - 群发短信全选功能

/**
 *  功能描述：
 *  群发短信全选功能显示全选按钮初始化
 *  创建时间：16/4/20
 *
 *  更新时间：2017/02/14 下午
 *  更新人： wangguiping
 */
- (void)massSelectViewInitWithShowUI {
    UIView *hView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 0.5)];
    hView.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    _massSelectView = [[UIView alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT-45-52-SafeAreaTopHeight, EMM_WIDTH, 50)];
    [self.view addSubview:_massSelectView];
    [_massSelectView addSubview:hView];
    
    UIButton *massSelectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    massSelectBtn.frame = CGRectMake(0, 0, EMM_WIDTH, 50);
    [massSelectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    massSelectBtn.backgroundColor = [UIColor colorFromHexCode:@"#f97242"];
    massSelectBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [massSelectBtn addTarget:self action:@selector(massSelectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_massSelectView addSubview:massSelectBtn];
    
    _massSelectImageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 20, 20)];
    _massSelectImageV.image = [UIImage imageNamed:@"choose_no"];
    [_massSelectView addSubview:_massSelectImageV];
    
    _massSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 15, 100, 20)];
    _massSelectLabel.font = [UIFont systemFontOfSize:15];
    _massSelectLabel.text = @"全选";
    [_massSelectView addSubview:_massSelectLabel];
    
    _massSelectNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH-150, 15, 140, 20)];
    _massSelectNumLabel.font = [UIFont systemFontOfSize:15];
    _massSelectNumLabel.text = @"已选0条";
    _massSelectNumLabel.textAlignment = NSTextAlignmentRight;
    _massSelectNumLabel.textColor = [UIColor colorFromHexCode:@"#ff7200"];
    [_massSelectView addSubview:_massSelectNumLabel];
}

- (void)massSelectBtnClick:(UIButton *)btn {
    if (_isMassSlect == YES) {
        _massSelectLabel.text = nil;
        _massSelectImageV.image = [UIImage imageNamed:@"choose_no"];
        _massSelectLabel.text = @"全选";
        [_seleArr removeAllObjects];
        _isMassSlect = NO;
        _massSelectNumLabel.text = [NSString stringWithFormat:@"已选%ld条",_seleArr.count];
        [_touristsTableView reloadData];
    }else {
        [_seleArr removeAllObjects];
        _isMassSlect = YES;
        _massSelectLabel.text = nil;
        _massSelectImageV.image = [UIImage imageNamed:@"choose"];
        _massSelectLabel.text = @"取消全选";
        for (int i = 0; i < _dataArr.count; i++) {
            [_seleArr addObject:[NSString stringWithFormat:@"%d",i]];
        }
        _massSelectNumLabel.text = [NSString stringWithFormat:@"已选%ld条",_seleArr.count];
        [_touristsTableView reloadData];
    }
}

#pragma mark - 导航右按钮弹出框选择群发短信 -----
- (void)creatChangeinitWithShowUI {
    leftItemButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(btnLeftItemClick:)];
    leftItemButton.tintColor = [UIColor colorFromHexCode:@"#ff7200"];
    [self.navigationItem setLeftBarButtonItem:leftItemButton];
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(btnRightItmeClick:)];
    [self.navigationItem setRightBarButtonItem:rightItme];
    integer = 1;
    rightItme.tintColor = [UIColor colorFromHexCode:@"#ff7200"];
    [_touristsTableView reloadData];
}

#pragma mark - 选择取消按钮响应事件
/**
 *  功能描述：
 *  群发短信取消选择
 *  创建时间：16/4/20
 *
 *  更新时间：2017/02/14 下午
 *  更新人： wangguiping
 */
- (void)btnLeftItemClick:(UIBarButtonItem *)btn {
    self.touristsTableView.frame = CGRectMake(0, self.topSet, EMM_WIDTH, EMM_HEIGHT-self.topSet-49-SafeAreaTopHeight);
    [_massSelectView removeFromSuperview];
    _massSelectView = nil;
    
    [leftItemButton setTitle:@""];
    [self creatNavigationController];
    integer = 0;
    [_touristsTableView reloadData];
    [_seleArr removeAllObjects];

}

- (void)btnRightItmeClick:(UIBarButtonItem *)btn {
    if (_seleArr.count == 0) {
        [self showToast:@"请先勾选再确定"];
        return;
    }
    [leftItemButton setTitle:@""];
    [self creatNavigationController];
    integer = 0;
    [_touristsTableView reloadData];
    if (_isTour == YES) {
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (int i = 0; i < _seleArr.count; i++) {
            NSString *string = [_seleArr objectAtIndex:i];
            CustomerList *list = [_dataArr objectAtIndex:[string intValue]];
            [arr addObject:list.mobile];
        }
//        if (arr.count > 0) {
//           [self creatinitWithGetNoteArr:arr];
//        }
        
        [_seleArr removeAllObjects];
    }else {
        [_massSelectView removeFromSuperview];
        _massSelectView = nil;
        
        if (_zyDataArr.count == 0) {
            [self showToast:@"暂无经纪人可选"];
            return ;
        }
        ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"选项" titleNmae:@"经纪人" numLits:nil items:_zyDataArr];
        menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [[UIApplication sharedApplication].keyWindow addSubview:menuView];
        __weak ShowMenuView *weakMenuView = menuView;
        [menuView setTouchBlock:^(NSString *indet,NSString *number) {
            NSLog(@"%@",indet);
            if ([NSString isStringEmpty2:indet] == NO) {
                ConsultantList *consList = [_zyData objectAtIndex:[number intValue]-1];
                NSString *consultantId = consList.consultantUserId;
                NSMutableArray *arr = [[NSMutableArray alloc] init];
                for (int i = 0; i < _seleArr.count; i++) {
                    NSString *string = [_seleArr objectAtIndex:i];
                    CustomerList *list = [_dataArr objectAtIndex:[string intValue]];
                    [arr addObject:@{@"customerId":list.customerId}];
                }
                
                [self postDistributionConsultantApiConsultantUserId:consultantId customerList:arr];
                [_seleArr removeAllObjects];
            }
            [weakMenuView removeFromSuperview];
            
            
        }];
    }
//
}

- (void)postDistributionConsultantApiConsultantUserId:(NSString *)consultantUserId customerList:(NSArray *)customerList {
    [_seleArr removeAllObjects];
    DistributionConsultantApi *api = [[DistributionConsultantApi alloc] initWithConsultantUserId:consultantUserId customerList:customerList];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        DistributionConsultantApi *result = (DistributionConsultantApi *)request;
        if (result.isSuccess) {
//            DistributionConsultant *get = result.resultData;
            [self showToast:@"分配成功"];
            [self.touristsTableView.mj_header beginRefreshing];
        } else {
            
            [self showToast:result.resultData.errorMsg];
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

#pragma mark - 跳出层初始化
- (void)creatDropDownView {
    
    
    CGFloat height = 40;
    UIView *topView = [[UIView alloc]init];
    
    if ([_suanjiaTpye isEqualToString:@"1"]) {
        height = 0;
        topView.hidden = YES;
    }
    topView.frame = CGRectMake(0, 0, EMM_WIDTH, height);
    topView.backgroundColor = [UIColor colorFromHexCode:@"#f3f3f3"];
    [self.view addSubview:topView];
    
    FuzzySearchView *searchView = [[FuzzySearchView alloc]initWithFrame:CGRectMake(20, 5, topView.frame.size.width - 40, 30) placeholderText:@"请输入客户姓名或手机号码" inputTextDidChange:^(NSString *text) {
    } cancelClick:^{
        
    }];
    searchView.searchViewDidEditClick = ^{
        EMMSearchViewController *nvc = [[EMMSearchViewController alloc] init];
        nvc.hidesBottomBarWhenPushed = YES;
        nvc.tpye = self.tpye;
        [self.navigationController pushViewController:nvc animated:YES];
    };
    searchView.hiddenCancel = YES;
    [topView addSubview:searchView];
    
    self.headBackView = [[UIView alloc] initWithFrame:CGRectMake(0,topView.frame.size.height, EMM_WIDTH, 45)];
    self.headBackView.backgroundColor = [UIColor colorFromHexCode:@"#f3f3f3"];
    [self.view addSubview:self.headBackView];
    self.topSet = topView.frame.size.height + self.headBackView.frame.size.height;
    
    UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 44.5, EMM_WIDTH, 0.5)];
    l1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [self.headBackView addSubview:l1];
    
    NSArray *titleArr = @[@"排序",@"筛选"];
    for (NSInteger i=0; i<titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(EMM_WIDTH/2 * i, 0, EMM_WIDTH/2, 45);
      
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        
        btn.tag = 2001 + i;
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width+EMM_WIDTH/2-60, 0, btn.titleLabel.bounds.size.width)];
        
        [btn addTarget:self action:@selector(headButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            self.djBtn = btn;
            [btn setImage:[UIImage imageNamed:@"ico_jt_up"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
        }if (i == 1) {
            self.btn = btn;
            [btn setImage:[UIImage imageNamed:@"shaixuan_up"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
        }
        [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
        [self.headBackView addSubview:btn];
    }
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH/2, 5, 1, 30)];

    label.backgroundColor = [UIColor colorFromHexCode:@"#e4e4e4"];
    [self.headBackView addSubview:label];
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
    CGSize textSize = [@"排序" boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    _headImageview = [[UIImageView alloc] initWithFrame:CGRectMake((EMM_WIDTH/2-textSize.width)/2-10, 43, textSize.width, 2)];
     _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#5A5A5A"];
    [self.headBackView addSubview:self.headImageview];
}



#pragma mark - 顶部按键响应事件
- (void)headButtonClick:(UIButton *)button {
    if (![self.suanjiaTpye isEqualToString:@"1"]) {
        [self btnLeftItemClick:nil];
    }
    [self headLineViewScrollWithIndex:button.tag-2001];
    if (touchIndex != button.tag || !_showView1) {
        if (!_showView1) {

            [self isCreatWithTag:button.tag-2000];
        }else {
            
            [self removeSubView];
            [self headLineViewScrollWithIndex:button.tag-2001];
            [self isCreatWithTag:button.tag-2000];
        }
        touchIndex = button.tag;
    }else if (touchIndex == button.tag && _showView1) {
        [self removeSubView];
        touchIndex = button.tag;
        if (button.tag == 2001) {
            if (_showView1 == nil) {
                [_djBtn setImage:[UIImage imageNamed:@"ico_jt_up"] forState:UIControlStateNormal];
            }else {
                [_djBtn setImage:[UIImage imageNamed:@"ico_jt_down"] forState:UIControlStateNormal];
            }
        }else {
            if (_showView1 == nil) {
                [_btn setImage:[UIImage imageNamed:@"shaixuan_up"] forState:UIControlStateNormal];
            }else {
                [_btn setImage:[UIImage imageNamed:@"shaixuan_down"] forState:UIControlStateNormal];
                
            }
        }
    }
    [_btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
    [_djBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];

}


- (void)isCreatWithTag:(NSInteger)btnTag {
    NSInteger h = 0;
    if (btnTag == 1) {
        h = _itmes.count * 45;
    }else {
        h = 6*45;
    }
    _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    _showView1 = [[ShowMenuListView alloc] initWithFrame:CGRectMake(0, 45+SafeAreaTopHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-SafeAreaTopHeight) title:nil titleNmae:[NSString stringWithFormat:@"%ld",btnTag] numLits:_itmeslist nameArr:_itmesDJ items:_itmes count:current countSub:currentSub countSubOne:currentSubOne gaodu:h];
    _showView1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:_showView1];
    
    __weak EMMTouristsViewController *weakSelf = self;
    [_showView1 setTouchBlock:^(NSString *inputText, NSString *numString, NSString *numSubOneString) {
        if (btnTag == 1) {
             [weakSelf.djBtn setImage:[UIImage imageNamed:@"ico_jt_up"] forState:UIControlStateNormal];
        }else {
            [weakSelf.btn setImage:[UIImage imageNamed:@"shaixuan_up"] forState:UIControlStateNormal];
        }
        
        /*
            排序
         */
        if ([inputText isEqualToString:@"tap"]) {
            
        }else {
            if ([NSString isEnglishOrNumbers:inputText] == YES) {
                if ([NSString isStringEmpty2:inputText] == NO) {
                    current = [inputText integerValue];
                    sort = [NSString stringWithFormat:@"%ld",[inputText integerValue] + 1];
                    _currentPageSize = 10;
                    [weakSelf.touristsTableView.mj_header beginRefreshing];
                }
            }
        }
        
        if ([numString isEqualToString:@"tap1"]) {
            
        }else {
            if ([NSString isStringEmpty2:numString] == NO) {
                
                currentSub = [numString integerValue];
                screenType = [NSString stringWithFormat:@"%ld",[numString integerValue] + 1];
                
            }
        }
        /*
         筛选
         */
        if ([numSubOneString isEqualToString:@"tap2"]) {

        }else {
            if ([NSString isStringEmpty2:numSubOneString] == NO) {
                saiXString = inputText;
                currentSubOne = [numSubOneString integerValue];
                NSArray *array = [inputText componentsSeparatedByString:@"|"]; //从字符A中分隔成2个元素的数组
                if ([array[0] isEqualToString:@"归属人"]) {
                    if (_gsDataArr.count > 0) {
                        ConsultantList *consList = [weakSelf.gsDataArr objectAtIndex:[numSubOneString integerValue]];
                        screenValue = consList.consultantUserId;
                    }else {
                        NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
                        screenValue =  [userdf objectForKey:@"userNameId"];
                    }
                }else if ([array[0] isEqualToString:@"首次接触方式"]){
                    for (int i = 0; i < _tcfsList.count; i++) {
                        RqBusNumList *list = [weakSelf.tcfsList objectAtIndex:i];
                        if ([array[1] isEqualToString:list.dictionaryName]) {
                            screenValue = list.dictionaryId;
                        }
                    }
                }else if ([array[0] isEqualToString:@"客户状态"]){
//                    arr1 = [NSArray arrayWithObjects:@"问询", @"来访",@"认筹",@"认购",@"签约",@"无意向",nil];
                    if ([array[1] isEqualToString:@"全部"]) {
                        screenValue = @"0";
                    }else if ([array[1] isEqualToString:@"问询"]) {
                        screenValue = @"1";
                    }else if ([array[1] isEqualToString:@"来访"]) {
                        screenValue = @"2";
                    }else if ([array[1] isEqualToString:@"认筹"]) {
                        screenValue = @"3";
                    }else if ([array[1] isEqualToString:@"认购"]) {
                        screenValue = @"4";
                    }else if ([array[1] isEqualToString:@"签约"]) {
                        screenValue = @"5";
                    }else if ([array[1] isEqualToString:@"无意向"]) {
                        screenValue = @"6";
                    }
                }else if ([array[0] isEqualToString:@"意向等级"]){
                    if ([array[1] isEqualToString:@"全部"]) {
                        screenValue = @"0";
                    }else {
                        screenValue = [NSString stringWithFormat:@"%ld",[numSubOneString integerValue]+1];
                    }
                } else {
                    screenValue = [NSString stringWithFormat:@"%ld",[numSubOneString integerValue]];
                }
               
                [weakSelf.touristsTableView.mj_header beginRefreshing];
               
            }
        }
        [weakSelf.btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
        [weakSelf.djBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
        [weakSelf removeSubView];
    }];
}

- (void)removeSubView {
    [_djBtn setImage:[UIImage imageNamed:@"ico_jt_up"] forState:UIControlStateNormal];
    [_btn setImage:[UIImage imageNamed:@"shaixuan_up"] forState:UIControlStateNormal];

    [self.djBtn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
    _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#5A5A5A"];
   
    [_showView1 removeFromSuperview];
    _showView1 = nil;
}

#pragma mark - 顶部线移动,
- (void)headLineViewScrollWithIndex:(NSInteger)index {
    [UIView animateWithDuration:0.15 animations:^{
        if (index == 0) {
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            CGSize textSize = [@"排序" boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
            _headImageview.frame = CGRectMake((EMM_WIDTH/2-textSize.width)/2-10, 43, textSize.width, 2);
            _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
            [self changeTitleColorAndWay1];
        }else if (index == 1) {
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            CGSize textSize = [@"筛选" boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
            _headImageview.frame = CGRectMake((EMM_WIDTH/2-textSize.width)/2+EMM_WIDTH/2-10, 43, textSize.width, 2);
            _headImageview.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];

            [self changeTitleColorAndWay2];
        }
    }];
}
//改变箭头方向和字体颜色
- (void)changeTitleColorAndWay1 {
    
    [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#ff7200"] forState:UIControlStateNormal];
    [_btn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
    [_djBtn setImage:[UIImage imageNamed:@"ico_jt_down"] forState:UIControlStateNormal];
    [_btn setImage:[UIImage imageNamed:@"shaixuan_up"] forState:UIControlStateNormal];
    
}

- (void)changeTitleColorAndWay2{
    [_djBtn setTitleColor:[UIColor colorFromHexCode:@"#5A5A5A"] forState:UIControlStateNormal];
    [_btn setTitleColor:[UIColor colorFromHexCode:@"#ff7200"] forState:UIControlStateNormal];
    [_djBtn setImage:[UIImage imageNamed:@"ico_jt_up"] forState:UIControlStateNormal];
    [_btn setImage:[UIImage imageNamed:@"shaixuan_down"] forState:UIControlStateNormal];
}

#pragma mark 下拉刷新数据
- (void)loadNewData {
    [self showHudInWindow:@"正在加载请稍等..."];
    [_dataArr removeAllObjects];
    _currentPageSize = 1;
    [self postGetCustomerListApiCurrentPageSize:_currentPageSize];
}

#pragma mark 上拉加载更多数据
- (void)loadMoreData {
    [self showHudInWindow:@"正在加载请稍等..."];
    _currentPageSize = _currentPageSize + 1;
    [self postGetCustomerListApiCurrentPageSize:_currentPageSize];
}

#pragma mark - 结束刷新
- (void)endRefreshing {
    [self.touristsTableView.mj_footer endRefreshing];
    [self.touristsTableView.mj_header endRefreshing];
}

#pragma mark -
- (void)postGetCustomerListApiCurrentPageSize:(int)currentPageSize {
    
    if (_dataArr.count <= 0) {
        _touristsTableView.hidden = YES;
    }
    
    /*
     type	类型	String(1)	类型：1、待处理客户；2、逾期未签约；3、逾期未付款 4代表未完善信息客户
     sort	排序	String(1)	排序：1、最新创建；2、最新更新(默认)；3、最新跟进；4、按标星 5 按客户等级
     screenType	筛选类型	String(1)	筛选类型：1、客户状态；2、意向等级；3、跟进时间；4、首次接触方式；5、归属人
     screenValue	筛选类型对应值	String(20)	screenType等于1时：1 来电、2 问询、3 认筹（无房）、4 小定、5  认购、6 签约、 7 无意向
     screenType等于2时：1 A+、2 A、3 B、4 C、5 D；
     screenType等于3时：1、3天未跟进；2、一周未跟进；3、两周未跟进；4、一月未跟进；5、两月未跟进；
     screenType等于4时：1 来电、2 来访、 3 其他。
     screenType等于5时：归属人Id
     
     */
    NSString *strTpye = nil;
    if ([_tourSring isEqualToString:@"0"]|| [_tourSring isEqualToString:@"1"] || [_tourSring isEqualToString:@"3"] || [_tourSring isEqualToString:@"4"]) {
        if ([screenType isEqualToString:@"3"]) {
            strTpye = @"4";
        }else {
            strTpye = screenType;
        }
    }else {
        strTpye = screenType;
    }
    GetCustomerListApi *api = [[GetCustomerListApi alloc] initWithType:self.tpye
                                                                  sort:sort
                                                            screenType:strTpye
                                                           screenValue:screenValue
                                                            pageNumber:currentPageSize
                                                              pageSize:10];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetCustomerListApi *result = (GetCustomerListApi *)request;
        if (result.isSuccess) {
            GetCustomerList *get = result.resultData;
            for (CustomerList *list in get.customerList) {
                [_dataArr addObject:list];
            }
            if (_dataArr.count > 0) {
                _jlLabel.hidden = YES;
                _touristsTableView.hidden = NO;
            }else {
                _jlLabel.hidden = NO;
                _touristsTableView.hidden = NO;
            }
            //yes 表示全选 no取消全选
            if (_seleArr.count == _dataArr.count) {
                _isMassSlect = YES;
                _massSelectLabel.text = nil;
                _massSelectImageV.image = [UIImage imageNamed:@"choose"];
                _massSelectLabel.text = @"取消全选";
                _massSelectNumLabel.text = [NSString stringWithFormat:@"已选%ld条",_seleArr.count];
            }else {
                _isMassSlect = NO;
                _massSelectLabel.text = nil;
                _massSelectImageV.image = [UIImage imageNamed:@"choose_no"];
                _massSelectLabel.text = @"全选";
                _massSelectNumLabel.text = [NSString stringWithFormat:@"已选%ld条",_seleArr.count];
            }
            
            [self hideHud];

        } else {
            [self hideHud];

            _jlLabel.hidden = NO;
            NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
            NSString *projectCode = [userdf objectForKey:@"projectCode"];
            if ([NSString isStringEmpty2:projectCode] == NO) {
                if (result.resultData.errorMsg) {
                    [self showToast:result.resultData.errorMsg];
                }else {
                    [self showToast:@"暂未获取到数据"];
                }
            }
            
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }

        }
        [self hideHud];
        [_touristsTableView reloadData];
        [self endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        _touristsTableView.hidden = YES;
        _jlLabel.hidden = NO;
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
        [self endRefreshing];
    }];
}

#pragma  mark - 证件号类型
- (void)postGetDictionaryListApi {
    GetDictionaryListApi *api = [[GetDictionaryListApi alloc] initWithBusNum:@"01" busNumType:@"11"];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetDictionaryListApi *result = (GetDictionaryListApi *)request;
        if (result.isSuccess) {
            GetDictionaryList *get = result.resultData;
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            [arr addObject:@"全部"];
            RqBusNumList *list = [[RqBusNumList alloc] init];
            list.dictionaryId = @"0";
            list.dictionaryName = @"全部";
            [_tcfsList addObject:list];
            for (RqBusNumList *list in get.rqBusNumList) {
                [arr addObject:list.dictionaryName];
                [_tcfsList addObject:list];
            }
            if (arr.count > 0) {
                [self creaShowUI:jjrindex tcfsList:arr];
            }

        } else {
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self creatNavigationController];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
    }];
}

#pragma  mark - 经纪人
/**
 *  功能描述：
 *  查询经纪人列表Api接口
 *  更新时间：2016/12/22
 *  更新人：王贵平
 *
 *  修改描述：修改条件选择内容信息新增了两个数组、_gsDataArr 、_gsDataNameArr;
 *  修改时间：2016/12/29
 *  修改人：王贵平
 */
- (void)postGetConsultantListApi {
    [_zyDataArr removeAllObjects];
    [_zyData removeAllObjects];
    [_gsDataArr removeAllObjects];
    [_gsDataNameArr removeAllObjects];

    GetConsultantListApi *api = [[GetConsultantListApi alloc] initWithResourcesCode:@"menu_0006_0001" searchType:@"0"];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetConsultantListApi *result = (GetConsultantListApi *)request;
        if (result.isSuccess) {
            GetConsultantList *get = result.resultData;
            _getConsultantList = get;
            ConsultantList *listNew = [[ConsultantList alloc] init];
            listNew.consultantUserName = @"全部";
            listNew.consultantUserId = @"0";
            [_gsDataArr addObject:listNew];
            [_gsDataNameArr addObject:listNew.consultantUserName];
            
            for (ConsultantList *list in get.consultantList) {
                [_zyDataArr addObject:list.consultantUserName];
                [_zyData addObject:list];
                [_gsDataArr addObject:list];
                [_gsDataNameArr addObject:list.consultantUserName];
            }
            jjrindex = 1;
            [self creaShowUI:1 tcfsList:nil];
            [self postGetDictionaryListApi];

        } else {
            jjrindex = 2;
            [self creaShowUI:2 tcfsList:nil];
            [self postGetDictionaryListApi];
//            [self showToast:result.resultData.errorMsg];
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
        jjrindex = 2;
        [self creaShowUI:2 tcfsList:nil];
        [self postGetDictionaryListApi];

    }];
}


#pragma mark - UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (integer != 0) {
        NSString *cellIdone = @"EMMTouristsEditCellId";
        EMMTouristsEditCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdone];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"EMMTouristsEditCell" owner:self options:nil] lastObject];
        }
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (_dataArr.count > 0) {
            CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
            
            if (integer == 1) {
                cell.imXuanz.hidden = NO;
                NSString *string = [NSString stringWithFormat:@"%ld",indexPath.row];
                if ([_seleArr containsObject:string]) {
                    cell.imXuanz.image = [UIImage imageNamed:@"choose"];
                } else {
                    cell.imXuanz.image = [UIImage imageNamed:@"choose_no"];
                }
            }else if (integer == 2) {
                cell.imXuanz.hidden = NO;
                if ([NSString isStringEmpty2:suanjiaString] == NO) {
                    if ([suanjiaString integerValue] == indexPath.row) {
                        cell.imXuanz.image = [UIImage imageNamed:@"choose"];
                    }else {
                        cell.imXuanz.image = [UIImage imageNamed:@"choose_no"];
                    }
                }else {
                    cell.imXuanz.image = [UIImage imageNamed:@"choose_no"];
                }
                
            }else if (integer == 0) {
                cell.imXuanz.hidden = YES;
            }
            
            if ([list.star isEqualToString:@"1"]) {
                cell.xjImageV.hidden = NO;
            }else {
                cell.xjImageV.hidden = YES;
            }
            if ([NSString isStringEmpty2:list.customerName]) {
                cell.khNameLabel.text = @"";
            }else{
                cell.khNameLabel.text = list.customerName;
            }
            if ([NSString isStringEmpty2:list.intentionLevel]) {
                cell.khDJLabel.text = @"";
            }else{
                cell.khDJLabel.text = [NSString stringWithFormat:@"(%@)",list.intentionLevel];
            }
            
            if ([NSString isStringEmpty2:list.customerStatus]) {
                cell.khZTLabel.text = @"";
            }else{
                cell.khZTLabel.text = list.customerStatus;
            }
            if ([NSString isStringEmpty2:list.customerOrigin]) {
                cell.khLYLabel.text = @"";
            }else{
                cell.khLYLabel.text = list.customerOrigin;
            }
            if ([NSString isStringEmpty2:list.attributionName]) {
                cell.gsNameLabel.text = @"";
            }else{
                cell.gsNameLabel.text = list.attributionName;
            }
            if ([NSString isStringEmpty2:list.createTime]) {
                cell.khTimerLabel.text = @"";
            }else{
                cell.khTimerLabel.text = list.createTime;
            }
            CGFloat width = cell.khNameLabel.frame.size.width - 150;
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
            CGSize textSize = [cell.khNameLabel.text boundingRectWithSize:cell.frame.size options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
            if (textSize.width > width ) {
                cell.starLeadingConst.constant = 0;
                cell.nameHeightConst.constant = 50;
            }else {
                cell.nameHeightConst.constant = 20;
                cell.starLeadingConst.constant = (width - textSize.width)-20;
            }
            
            cell.khDJLabel.hidden = [list.dataAuthority isEqualToString:@"0"];
            cell.khZTLabel.hidden = [list.dataAuthority isEqualToString:@"0"];
        }
        return cell;
    }
    
    //未编辑状态
    NSString *cellId = @"EMMTouristsNewCellId";
    EMMTouristsNewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EMMTouristsNewCell" owner:self options:nil] lastObject];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dataArr.count > 0) {
        CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
        
        if ([list.star isEqualToString:@"1"]) {
            cell.xjImageV.hidden = NO;
        }else {
            cell.xjImageV.hidden = YES;
        }
        if ([NSString isStringEmpty2:list.customerName]) {
            cell.khNameLabel.text = @"";
        }else{
            cell.khNameLabel.text = list.customerName;
        }
        
        if ([NSString isStringEmpty2:list.intentionLevel]) {
            cell.khDJLabel.text = @"";
        }else{
            cell.khDJLabel.text = [NSString stringWithFormat:@"(%@)",list.intentionLevel];
        }
        if ([NSString isStringEmpty2:list.customerStatus]) {
            cell.khZTLabel.text = @"";
        }else{
            cell.khZTLabel.text = list.customerStatus;
        }
        if ([NSString isStringEmpty2:list.customerOrigin]) {
            cell.khLYLabel.text = @"";
        }else{
            cell.khLYLabel.text = list.customerOrigin;
        }
        if ([NSString isStringEmpty2:list.attributionName]) {
            cell.gsNameLabel.text = @"";
        }else{
            cell.gsNameLabel.text = list.attributionName;
        }
        if ([NSString isStringEmpty2:list.createTime]) {
            cell.khTimerLabel.text = @"";
        }else{
            cell.khTimerLabel.text = list.createTime;
        }
        CGFloat width = cell.frame.size.width * 0.4;
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],};
        CGSize textSize = [cell.khNameLabel.text boundingRectWithSize:cell.frame.size options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
        if (textSize.width > width ) {
            cell.starLeadingCons.constant = width;
            cell.nameHeightCons.constant = 50;
        }else {
           cell.nameHeightCons.constant = 20;
          cell.starLeadingCons.constant =  textSize.width + 10;
        }
        
        cell.khDJLabel.hidden = [list.dataAuthority isEqualToString:@"0"];
        cell.khZTLabel.hidden = [list.dataAuthority isEqualToString:@"0"];
        
    }
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 每天权限
      CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
    if ([list.dataAuthority isEqualToString:@"0"]) {
        [self showToast:@"您没有查看权限"];
        return;
    }
    
    if (integer == 0) {
        if (_dataArr.count > 0) {
            CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
            TouristsDetailWebViewController *tvc = [[TouristsDetailWebViewController alloc] init];
            tvc.hidesBottomBarWhenPushed = YES;
            tvc.customerId = list.customerId;
            tvc.isNot = NO;
            [self.navigationController pushViewController:tvc animated:YES];
        }
    }else if(integer == 1) {
        NSLog(@"1");
        NSString *string = [NSString stringWithFormat:@"%ld",indexPath.row];
        if ([_seleArr containsObject:string]) {
            [_seleArr removeObject:string];
        } else {
            [_seleArr addObject:string];
        }
        if (_seleArr.count == _dataArr.count) {
            _isMassSlect = YES;
            _massSelectLabel.text = nil;
            _massSelectImageV.image = [UIImage imageNamed:@"choose"];
            _massSelectLabel.text = @"取消全选";
            _massSelectNumLabel.text = [NSString stringWithFormat:@"已选%ld条",_seleArr.count];
        }else {
            _isMassSlect = NO;
            _massSelectLabel.text = nil;
            _massSelectImageV.image = [UIImage imageNamed:@"choose_no"];
            _massSelectLabel.text = @"全选";
            _massSelectNumLabel.text = [NSString stringWithFormat:@"已选%ld条",_seleArr.count];
        }
        [_touristsTableView reloadData];
    }else if (integer == 2) {
        suanjiaString = [NSString stringWithFormat:@"%ld",indexPath.row];
        [_touristsTableView reloadData];
    }
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
    return [list.dataAuthority isEqualToString:@"1"];
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
    if ([_suanjiaTpye isEqualToString:@"1"] || [list.dataAuthority isEqualToString:@"0"]) {
        return nil;
    }
    
    NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
    NSString *isAddCustomer = [userdf objectForKey:@"isAddCustomer"];//客户菜单权限下按钮权限：客户新增
    NSString *isDistributionCustomer = [userdf objectForKey:@"isDistributionCustomer"];//客户菜单权限下按钮权限：客户分配
    //isAddCustomer = @"0"; isDistributionCustomer = @"0";
    if ([_tourSring isEqualToString:@"0"]|| [_tourSring isEqualToString:@"1"] || [_tourSring isEqualToString:@"3"] || [_tourSring isEqualToString:@"4"]) {
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"打电话" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了打电话");
            CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//            NSString *isPhoneInformation = [userdf objectForKey:@"isPhoneInformation"];
//            if ([NSString isStringEmpty2:isPhoneInformation] == NO) {
//                if ([isPhoneInformation isEqualToString:@"0"]) {
//                    if ([NSString isStringEmpty2:list.mobile] == NO) {
//                        [self creatinitWithGetPhone:list.mobile];
//                    }
//                }else if ([isPhoneInformation isEqualToString:@"1"]) {
//                    [self postGetUserCallListApi:list];
//                }
//            }else {
//                if ([NSString isStringEmpty2:list.mobile] == NO) {
//                    [self creatinitWithGetPhone:list.mobile];
//                }
//            }
            [self postGetUserCallListApi:list];

            
        }];
        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"首触" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了首触了。。。。。。");
            CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//            if ([NSString isStringEmpty2:list.mobile] == NO) {
//                NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:list.mobile, nil];
                [self creatinitWithGetNoteArr:list];
//            }
        }];
        
        sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"写跟进" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了写跟进");
            CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
            [self creatinitWithGetReadAfterWriteCustomerId:list.customerId
                                              customerName:list.customerName
                                                      star:list.star
                                            intentionLevel:list.intentionLevel
                                            customerOrigin:list.customerOrigin
                                            customerStatus:list.customerStatus
                                                    mobile:list.mobile
                                                       sex:list.customerSex];
        }];
        deleteRowAction.backgroundColor = [UIColor colorFromHexCode:@"#f82b49"];
        moreRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff6253"];
        sanRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
        return @[sanRowAction,moreRowAction,deleteRowAction];
    }else {
        if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"1"]) {
            deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"打电话" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了打电话");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                NSString *isPhoneInformation = [userdf objectForKey:@"isPhoneInformation"];
//                if ([NSString isStringEmpty2:isPhoneInformation] == NO) {
//                    if ([isPhoneInformation isEqualToString:@"0"]) {
//                        if ([NSString isStringEmpty2:list.mobile] == NO) {
//                            [self creatinitWithGetPhone:list.mobile];
//                        }
//                    }else if ([isPhoneInformation isEqualToString:@"1"]) {
//                        [self postGetUserCallListApi:list];
//                    }
//                }else {
//                    if ([NSString isStringEmpty2:list.mobile] == NO) {
//                        [self creatinitWithGetPhone:list.mobile];
//                    }
//                }
                [self postGetUserCallListApi:list];

                
            }];
            moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"首触" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了发短信");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                if ([NSString isStringEmpty2:list.mobile] == NO) {
//                    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:list.mobile, nil];
                    [self creatinitWithGetNoteArr:list];
//                }
            }];
            
            sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"写跟进" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了写跟进");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
                [self creatinitWithGetReadAfterWriteCustomerId:list.customerId
                                                  customerName:list.customerName
                                                          star:list.star
                                                intentionLevel:list.intentionLevel
                                                customerOrigin:list.customerOrigin
                                                customerStatus:list.customerStatus
                                                        mobile:list.mobile
                                                           sex:list.customerSex];
            }];
            
            OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"分配" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了分配");
                if (_zyDataArr.count == 0) {
                    [self showToast:@"暂无经纪人可选"];
                    return ;
                }
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
                ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"选项" titleNmae:@"经纪人" numLits:nil items:_zyDataArr];
                menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
                [[UIApplication sharedApplication].keyWindow addSubview:menuView];
                __weak ShowMenuView *weakMenuView = menuView;
                [menuView setTouchBlock:^(NSString *indet,NSString *number) {
                    NSLog(@"%@",indet);
                    if ([NSString isStringEmpty2:indet] == NO) {
                        
                        ConsultantList *consList = [_zyData objectAtIndex:[number intValue]-1];
                        NSString *consultantId = consList.consultantUserId;
                        NSArray *arr = @[@{@"customerId":list.customerId}];
                        
                        [self postDistributionConsultantApiConsultantUserId:consultantId customerList:arr];
                        
                    }
                    [weakMenuView removeFromSuperview];
                    
                }];
            }];
            deleteRowAction.backgroundColor = [UIColor colorFromHexCode:@"#f82b49"];
            moreRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff6253"];
            sanRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
            OK.backgroundColor = [UIColor colorFromHexCode:@"#ffb527"];
            return @[OK,sanRowAction,moreRowAction,deleteRowAction];
        }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"1"]) {
            deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"打电话" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了打电话");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                NSString *isPhoneInformation = [userdf objectForKey:@"isPhoneInformation"];
//                if ([NSString isStringEmpty2:isPhoneInformation] == NO) {
//                    if ([isPhoneInformation isEqualToString:@"0"]) {
//                        if ([NSString isStringEmpty2:list.mobile] == NO) {
//                            [self creatinitWithGetPhone:list.mobile];
//                        }
//                    }else if ([isPhoneInformation isEqualToString:@"1"]) {
//                        [self postGetUserCallListApi:list];
//                    }
//                }else {
//                    if ([NSString isStringEmpty2:list.mobile] == NO) {
//                        [self creatinitWithGetPhone:list.mobile];
//                    }
//                }
                [self postGetUserCallListApi:list];

            }];
            
            moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"首触" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了发短信");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                if ([NSString isStringEmpty2:list.mobile] == NO) {
//                    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:list.mobile, nil];
                    [self creatinitWithGetNoteArr:list];
//                }
            }];
            
            sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"写跟进" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了写跟进");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
                [self creatinitWithGetReadAfterWriteCustomerId:list.customerId
                                                  customerName:list.customerName
                                                          star:list.star
                                                intentionLevel:list.intentionLevel
                                                customerOrigin:list.customerOrigin
                                                customerStatus:list.customerStatus
                                                        mobile:list.mobile
                                                           sex:list.customerSex];
            }];
            
            OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"分配" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了分配");
                if (_zyDataArr.count == 0) {
                    [self showToast:@"暂无经纪人可选"];
                    return ;
                }
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
                ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"选项" titleNmae:@"经纪人" numLits:nil items:_zyDataArr];
                menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
                [[UIApplication sharedApplication].keyWindow addSubview:menuView];
                __weak ShowMenuView *weakMenuView = menuView;
                [menuView setTouchBlock:^(NSString *indet,NSString *number) {
                    NSLog(@"%@",indet);
                    if ([NSString isStringEmpty2:indet] == NO) {
                        
                        ConsultantList *consList = [_zyData objectAtIndex:[number intValue]-1];
                        NSString *consultantId = consList.consultantUserId;
                        NSArray *arr = @[@{@"customerId":list.customerId}];
                        
                        [self postDistributionConsultantApiConsultantUserId:consultantId customerList:arr];
                        
                    }
                    [weakMenuView removeFromSuperview];
                    
                }];
            }];
            deleteRowAction.backgroundColor = [UIColor colorFromHexCode:@"#f82b49"];
            moreRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff6253"];
            sanRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
            OK.backgroundColor = [UIColor colorFromHexCode:@"#ffb527"];
            return @[OK,sanRowAction,moreRowAction,deleteRowAction];
        }else if ([isAddCustomer isEqualToString:@"1"] && [isDistributionCustomer isEqualToString:@"0"]) {
            deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"打电话" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了打电话");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                NSString *isPhoneInformation = [userdf objectForKey:@"isPhoneInformation"];
//                if ([NSString isStringEmpty2:isPhoneInformation] == NO) {
//                    if ([isPhoneInformation isEqualToString:@"0"]) {
//                        if ([NSString isStringEmpty2:list.mobile] == NO) {
//                            [self creatinitWithGetPhone:list.mobile];
//                        }
//                    }else if ([isPhoneInformation isEqualToString:@"1"]) {
//                        [self postGetUserCallListApi:list];
//                    }
//                }else {
//                    if ([NSString isStringEmpty2:list.mobile] == NO) {
//                        [self creatinitWithGetPhone:list.mobile];
//                    }
//                }
                [self postGetUserCallListApi:list];
            }];
            moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"首触" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了发短信");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                if ([NSString isStringEmpty2:list.mobile] == NO) {
//                    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:list.mobile, nil];
                    [self creatinitWithGetNoteArr:list];
//                }
            }];
            
            sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"写跟进" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了写跟进");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
                [self creatinitWithGetReadAfterWriteCustomerId:list.customerId
                                                  customerName:list.customerName
                                                          star:list.star
                                                intentionLevel:list.intentionLevel
                                                customerOrigin:list.customerOrigin
                                                customerStatus:list.customerStatus
                                                        mobile:list.mobile
                                                           sex:list.customerSex];
            }];
            deleteRowAction.backgroundColor = [UIColor colorFromHexCode:@"#f82b49"];
            moreRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff6253"];
            sanRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
            return @[sanRowAction,moreRowAction,deleteRowAction];
        }else if ([isAddCustomer isEqualToString:@"0"] && [isDistributionCustomer isEqualToString:@"0"]) {
            deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"打电话" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了打电话");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                NSString *isPhoneInformation = [userdf objectForKey:@"isPhoneInformation"];
//                if ([NSString isStringEmpty2:isPhoneInformation] == NO) {
//                    if ([isPhoneInformation isEqualToString:@"0"]) {
//                        if ([NSString isStringEmpty2:list.mobile] == NO) {
//                            [self creatinitWithGetPhone:list.mobile];
//                        }
//                    }else if ([isPhoneInformation isEqualToString:@"1"]) {
//                        [self postGetUserCallListApi:list];
//                    }
//                }else {
//                    if ([NSString isStringEmpty2:list.mobile] == NO) {
//                        [self creatinitWithGetPhone:list.mobile];
//                    }
//                }
                [self postGetUserCallListApi:list];
            }];
            moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"首触" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了发短信");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
//                if ([NSString isStringEmpty2:list.mobile] == NO) {
//                    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:list.mobile, nil];
                    [self creatinitWithGetNoteArr:list];
//                }
            }];
            
            sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"写跟进" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                NSLog(@"点击了写跟进");
                CustomerList *list = [_dataArr objectAtIndex:indexPath.row];
                [self creatinitWithGetReadAfterWriteCustomerId:list.customerId
                                                  customerName:list.customerName
                                                          star:list.star
                                                intentionLevel:list.intentionLevel
                                                customerOrigin:list.customerOrigin
                                                customerStatus:list.customerStatus
                                                        mobile:list.mobile
                                                           sex:list.customerSex];
            }];
            deleteRowAction.backgroundColor = [UIColor colorFromHexCode:@"#f82b49"];
            moreRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff6253"];
            sanRowAction.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
            return @[sanRowAction,moreRowAction,deleteRowAction];
        }
    }
    
    return nil;
}

#pragma mark - 打电话
- (void)creatinitWithGetPhone:(NSString *)phonString {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phonString];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    NSLog(@"1");
}

#pragma mark - 发短信  修改为首触功能
- (void)creatinitWithGetNoteArr:(CustomerList *)userModel {
    [_massSelectView removeFromSuperview];
    _massSelectView = nil;
    // 跳转首触页面
    
    FirstTouchController *vc = [[FirstTouchController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.userModel = userModel;
    [self.navigationController pushViewController:vc animated:YES];
    
    
//    if( [MFMessageComposeViewController canSendText] ){
//        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init];
//        controller.recipients = dxArr;
//
//        controller.messageComposeDelegate = self;
//        [[UINavigationBar appearanceWhenContainedIn:[MFMessageComposeViewController class], nil] setBarTintColor:[UIColor colorFromHexCode:@"#346c6d"]];
//        [self presentViewController:controller animated:YES completion:^{
//
//        }];
//        [[[[controller viewControllers] lastObject] navigationItem] setTitle:nil];//修改短信界面标题
//    }else{
//        [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];
//    }
}



#pragma mark - MFMessageComposeViewControllerDelegate发短信协议代理方法调用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    [controller dismissViewControllerAnimated:NO completion:^{
    }];
    switch ( result ) {
        case MessageComposeResultCancelled:
            [self alertWithTitle:@"提示信息" msg:@"发送取消"];
            break;
        case MessageComposeResultFailed:// send failed
            [self alertWithTitle:@"提示信息" msg:@"发送失败"];
            break;
        case MessageComposeResultSent:
            [self alertWithTitle:@"提示信息" msg:@"发送成功"];
            break;
        default:
            break;
    }
}


#pragma mark - 写跟进
- (void)creatinitWithGetReadAfterWriteCustomerId:(NSString *)customerId
                                    customerName:(NSString *)customerName
                                            star:(NSString *)star
                                  intentionLevel:(NSString *)intentionLevel
                                  customerOrigin:(NSString *)customerOrigin
                                  customerStatus:(NSString *)customerStatus
                                          mobile:(NSString *)mobile
                                             sex:(NSString *)sex{
    FollowUpViewController *fvc = [[FollowUpViewController alloc] init];
    fvc.hidesBottomBarWhenPushed = YES;
    fvc.customerId = customerId;
    [self.navigationController pushViewController:fvc animated:YES];
}


#pragma mark - 弹出框
- (void)alertWithTitle:(NSString *)title msg:(NSString *)msg {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"好", nil];
    [alert show];
}

- (void)tourbtnClick:(UIButton *)btn {
    
}

- (void)postGetUserCallListApi:(CustomerList *)list {
    [self showHudInWindow:@"正在加载中..."];
    GetUserCallListApi *api = [[GetUserCallListApi alloc] initWith];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetUserCallListApi *result = (GetUserCallListApi *)request;
        //请求成功
        if (result.isSuccess) {
            GetUserCallList *get = result.resultData;
            
            if ([NSString isStringEmpty2:get.isPhoneInformation] == NO) {
                if ([get.isPhoneInformation isEqualToString:@"0"]) {
                    if ([NSString isStringEmpty2:list.mobile] == NO) {
                        [self creatinitWithGetPhone:list.mobile];
                    }
                }else {
                    UserCallMenuView *userListV = [[UserCallMenuView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT) title:@"选择拨打号码" nameArr:get.userCallList];
                    userListV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
                    [[UIApplication sharedApplication].keyWindow addSubview:userListV];
                    __weak UserCallMenuView *weakMenuView = userListV;
                    [userListV setTouchUserCallMenuViewBlock:^(NSString *telePhone, NSString *telePhoneType) {
                        NSLog(@"%@-%@",telePhone,telePhoneType);
                        if ([NSString isStringEmpty2:telePhone] == NO) {
                            [self postGetCallBackApi:list telePhoneString:telePhone];
                        }
                        [weakMenuView removeFromSuperview];
                    }];
                }
            }else {
                if ([NSString isStringEmpty2:list.mobile] == NO) {
                    [self creatinitWithGetPhone:list.mobile];
                }
                if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                    NSDictionary *dictionary = [defs dictionaryRepresentation];
                    for(NSString *key in [dictionary allKeys]){
                        if ([key isEqualToString:@"DeviceToken123"] ||
                            [key isEqualToString:@"loginText"] ||
                            [key isEqualToString:@"provinceCityDistrict"]) {
                        }else {
                            [defs removeObjectForKey:key];
                            [defs synchronize];
                        }
                    }
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                        [appInstance showLogin];
                    }];
                    [alertController addAction:cancelAction];
                    [self presentViewController:alertController animated:YES completion:nil];
                }
            }
            
            
        } else {

        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

- (void)postGetCallBackApi:(CustomerList *)list telePhoneString:(NSString *)telePhoneString {
    
    GetCallBackApi *api = [[GetCallBackApi alloc] initWithCustomerId:list.customerId caller:telePhoneString called:list.mobile terminalModel:@"0" terminalModelId:list.customerId];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetCallBackApi *result = (GetCallBackApi *)request;
        //请求成功
        if (result.isSuccess) {
            GetCallBack *get = result.resultData;
            FollowUpViewController *fvc = [[FollowUpViewController alloc] init];
            fvc.hidesBottomBarWhenPushed = YES;
            fvc.customerId = list.customerId;
            fvc.followId = get.followId;
            [self.navigationController pushViewController:fvc animated:YES];
        } else {
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}


@end
